<template lang="html">
    <div class="setup-content" app="index">
        <template>
            <!-- <el-card class="box-card"> -->
                <div slot="header" class="clearfix">
                    <div class="clearfix-left"> <span>商品收藏</span></div>
                    <div class="clearfix-right">
                        <el-button type="button" v-on:click="batchActive = 1" v-if="batchActive === 0 &&(data.list)&&data.list.length!=0" >批量操作</el-button>
                        <el-checkbox v-model="checked"  v-if="batchActive === 1" @change="selectAll(checked)">全选</el-checkbox>
                        <el-button type="button" v-if="batchActive === 1" v-on:click="batchDelete()" >批量删除</el-button>
                        <el-button type="button" v-on:click="canalDelete()"   v-if="batchActive === 1" >取消</el-button>
                    </div>
                </div>
                <div class="setup-content-form">
<!--                    v-on:click="selectActive(item)"-->
                    <div class="collet-div" v-for="item in data.list" v-on:mousemove="item.current = 1"  v-on:mouseleave="item.current = 0">
                        <div v-if=" batchActive === 1 ">
                            <div class="collet-div-check" v-on:click="ckActiveName(item)" :class="{active:item.active == '1'}">
                                <img src="@/assets/images/member/gou.png">
                            </div>
                        </div>
                        <div class="collet-div-batchActive" v-if=" batchActive === 1" :class="{active:item.active == '1'}">

                        </div>
                        <div class="collet-div-active" v-if=" batchActive === 0 && item.current === 1 ">
                            <div class="collet-div-button" @click="routeToPath('/product/' + item.favoriteId)">查看商品</div>
                            <div class="collet-div-button" @click="cancelCollect(item)">取消收藏</div>
                        </div>
                        <div class="collet-div-img">
                            <img class="setup-content-form-head-img" v-if="item.content!=null && item.content!=undefined" :src="URL.PIC+item.content.image" alt="" >
                        </div>
                        <div class="collet-div-title">
                            <span :title="item.content.title" v-if="item.content!=null && item.content!=undefined">{{item.content.title}}</span>
                        </div>
                        <div class="collet-div-price">
                             <span v-if="item.content!=null && item.content!=undefined">¥ {{item.content.sellingPrice}}</span>
                        </div>
                    </div>
                </div>

                <div class="order-nodata" v-if="(!data.list)||data.list.length===0">
                    <img src="@/assets/images/member/notcollect.png" />
                    <p>暂时还没有收藏哦~</p>
                </div>
                <div class="collet-foot" v-if="(data.list)&&data.list.length!=0">
                    <div class="collet-pagination">
                        <template>
                            <el-pagination
                                    background
                                    layout="prev, pager, next"
                                    :current-page.sync="currentPage"
                                    :total="data.total" :page-count="data.pages" :page-size = "size" @current-change="currentPagination" @prev-click="prevPagination" @next-click="nextPagination">
                            </el-pagination>
                            <div class="demo-input-suffix"> 共 {{data.pages}} 页 到第 <el-input type="number" v-model="nowPage" /> 页 <el-button @click="actionPage()" type="button">确定</el-button> </div>
                        </template>
                    </div>
                </div>
<!--            </el-card>-->
        </template>

    </div>
</template>

<script>

    import {getColletProduct,removeColletProduct,batchDelete} from '@/api/member';

    export default {
        name:'index',
        data() {
            return {
                page:1,
                size:8,
                data:[],
                nowPage:1,
                currentPage:1,
                batchActive:0,
                activeNames:1,
                checked: false,
            }
        },
        created(){
            this.colletList(this.page,this.size);
        },
        methods: {
            colletList(page,size) {
                this.batchActive = 0;
                getColletProduct({page:page,size:size}).then(res=>{
                    var datas = res.data.list;
                    datas.forEach(v=>{
                        v.active = 0;
                    });
                    this.data = res.data;
                });
            },
            errorMsg(res){
                this.$message.error('保存失败'+res.msg);
            },
            onSubmit(){


            },
            //全选
            selectAll(checked){
                if(checked === true){
                    var datas = this.data.list;
                    datas.forEach(v=>{
                        v.active = 1;
                    });
                }else{
                    var datas = this.data.list;
                    datas.forEach(v=>{
                        v.active = 0;
                    });
                }
            },
            canalDelete(){
                this.batchActive = 0;
                var datas = this.data.list;
                datas.forEach(v=>{
                    v.active = 0;
                });
            },
            //批量删除
            batchDelete(){
                //获取批量选中的收藏商品
                var fids = [];
                var datas = this.data.list;
                datas.forEach(v=>{
                    if(v.active === 1){
                        fids.push(v.favoriteId);
                    }
                });
                if(fids.length === 0){
                    this.$message({
                        message: '请选中需要批量删除的收藏商品。',
                        type: 'info'
                    });
                    return;
                }

                this.$confirm('确定进行批量删除商品操作吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    var ids = fids.join(',');
                    batchDelete({favoriteIds:ids}).then(res =>{
                        if(res.code === 200){
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                        }
                        this.colletList(this.page,this.size);
                    });
                }).catch(() => {
                });
            },
            //取消收藏
            cancelCollect(item){
                this.$confirm('确定将'+item.content.title+'商品取消收藏吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    removeColletProduct({favoriteId:item.favoriteId}).then(res =>{
                        if(res.code === 200){
                            this.$message({
                                message: '取消成功',
                                type: 'success'
                            });
                        }
                        this.colletList(this.page,this.size);
                    });
                }).catch(() => {
                });
            },
            selectActive(item){
                if(item.current === 1){
                    item.current = 0;
                }else{
                    item.current = 1;
                }
            },
            ckActiveName(item){
                if(item.active === 0){
                    item.active = 1;
                }else{
                    if(item.active === undefined){
                        item.active = 1;
                    }else{
                        item.active = 0;
                    }

                }
            },
            currentPagination(data){
                this.colletList(data,this.size);
            },
            prevPagination(data){
                this.colletList(data,this.size);
            },
            nextPagination(data){
                this.colletList(data,this.size);
            },
            actionPage(){
                //跳转
                if(this.data.pages >= this.nowPage && this.nowPage > 0){
                    this.page = this.nowPage;
                    this.currentPage = this.nowPage;
                }else{
                    if(this.data.pages<this.nowPage){
                        this.page = this.data.pages;
                        this.nowPage = this.data.pages;
                        this.currentPage = this.data.pages;
                    }else{
                        this.page = 1;
                        this.nowPage = 1;
                        this.currentPage = 1;
                    }
                }
                this.colletList(this.page,this.size);
            },
        }
    };
</script>

<style lang="scss" scoped>

    .order-nodata{
        padding: 140px 0;
        text-align: center;
        p{
            font-size: 14px;
            color: #888;
            margin-top: 20px;
        }
    }

    .collet-pagination{
        clear: both;
        float: right;
        right: 4%;
        position: relative;
    }

    .demo-input-suffix {
        padding: 25px 0px;
        white-space: normal;
        height: 25px;
        display: inline-block;
        float: left;
        .el-input {
            width: 70px;
        }
    }

    .setup-content{
        margin-left: 200px;
        margin-top: 20px;
        margin-bottom: 20px;
        background: #fff;
        border: 1px solid #eee;
        .collet-foot{
            background: #fff;
            width: 100%;
            display: inline-block;
        }
    }

    .el-pagination{
        padding: 30px 0px;
        white-space: normal;
        height: 30px;
        display: inline-block;
        float: left;
    }

    .clearfix{
        font-size: 16px;
        height: 56px;
        line-height: 56px;
        overflow: hidden;
        padding: 0 20px;
        border-bottom: 1px solid #eee;
        .clearfix-left{
            width: 30%;
            text-align: left;
            float: left;
        }
        .clearfix-right{
            width: 69%;
            text-align: right;
            float: right;
        }
    }
    .clearfix:before,
    .clearfix:after {
        display: table;
        content: "";
    }
    .clearfix:after {
        clear: both
    }

    .box-card {
        width: 100%;
    }

    .collet-div{
        height:260px;
        border:1px solid rgba(238,238,238,1);
        float: left;
        width: 20%;
        margin: 30px 2.3% 0px 2%;
        .collet-div-batchActive{
            opacity: 0.4;
            position: absolute;
            z-index: 0;
            width: 195px;
            height: 260px;
            background: black;
            &.active{
                background: #255B8B;
                border: 1px solid #255B8B;
            }
        }
        .collet-div-check {
            width: 40px;
            height: 40px;
            background: black;
            opacity: 0.5;
            position: absolute;
            z-index: 1;
            margin-left: 155px;
            text-align: center;
            font-size: 25px;
            line-height: 40px;
            color: #fff;
            cursor: pointer;
            &.active{
                background: #255B8B;
                opacity: 1;
            }
        }
        .collet-div-active{
            opacity: 0.6;
            position: absolute;
            z-index: 1;
            width: 195px;
            .collet-div-button{
                width:49%;
                height:30px;
                background:rgba(0,0,0,1);
                float: left;
                font-size:14px;
                font-family:PingFang-SC-Medium;
                font-weight:500;
                color:rgba(255,255,255,1);
                line-height:30px;
                text-align: center;
                cursor: pointer;
            }
        }
        .collet-div-button:nth-child(2){
            margin-left: 2%;
        }
        .collet-div-img{
            width:140px;
            height:140px;
            border-radius:10px;
            margin: 0 auto;
            padding-top: 20px;
            img{
                width: 99%;
                height: 99%;
            }
        }
        .collet-div-title{
            padding-top: 20px;
            width: 154px;
            height: 34px;
            font-size: 12px;
            font-family: PingFang-SC-Medium;
            font-weight: 500;
            color: #333333;
            line-height: 16px;
            text-align: center;
            margin: 0 auto;
            display: -webkit-box;
            -webkit-box-orient: vertical;
            -webkit-line-clamp: 2;
            overflow: hidden;
        }
        .collet-div-price{
            width:78px;
            height:15px;
            font-size:16px;
            font-family:PingFang-SC-Medium;
            font-weight:500;
            color:rgba(208,2,27,1);
            line-height:20px;
            padding-top: 10px;
            text-align: center;
            margin: 0 auto;
        }
    }

    .avatar-uploader{
        float: left;
        .el-button--primary{
            background-color: #DDDDDD;
            border-color: #DDDDDD;
        }
    }

    .setup-content-submit{
        width:120px;
        height:40px;
        background:#255B8A;
        border-radius:4px;
    }

    .setup-content-form{
        color: #666666;
        padding-left: 2.2%;
        background: #fff;
        .setup-content-form-head {
            .upload-demo {
                div:nth-child(1) {
                    float: left;
                }
            }
        }
        .setup-content-form-head-img{
            width:100px;
            height: 100px;
        }

        .setup-content-form-head-tig{
            padding-left: 100px;
            color: #888888;
            font-size: 12px;
        }

        .el-form-item{
            padding-top: 8px;
            margin-bottom: 0px;
        }
    }

    .el-button--text{
        width:84px;
        height:13px;
        font-size:14px;
        font-family:PingFang-SC-Medium;
        font-weight:500;
        color:#255B8B;
    }

    .el-form-item__label{
        font-size: 14px;
        color: #666666;
        font-family:PingFang-SC-Medium;
        font-weight:500;
    }

    .nickname{
        width: 30%;
    }
</style>
